package com.example.tests;

import static org.junit.Assert.*;
import java.util.*;

import static org.hamcrest.Matchers.*;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class GroupCreationTests extends TestBase {

	Random rnd = new Random();

	@DataProvider(name = "randomGroups")
	public Iterator<Object[]> generateRandomGroups() {
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++) {
			GroupObject group = new GroupObject()
					.setHeader("header" + rnd.nextInt())
					.setName("name" + rnd.nextInt())
					.setFooter("footer" + rnd.nextInt());
			list.add(new Object[] { group });
		}
		return list.iterator();
	}

	@Test(dataProvider = "randomGroups")
	public void testValidGroupCanBeCreated(GroupObject validGroup)
			throws Exception {

		Set<GroupObject> oldList = app.getGroupHelper().getGroups();
		// action
		app.getGroupHelper().createGroup(validGroup);
		// verification
		Set<GroupObject> newList = app.getGroupHelper().getGroups();
		verifyGroupAdded(oldList, validGroup, newList);
	}

	private void verifyGroupAdded(Set<GroupObject> oldList,
			GroupObject validGroup, Set<GroupObject> newList) {
		assertThat(newList.size(), equalTo(oldList.size() + 1));
		validGroup.setId("$$$");
		oldList.add(validGroup);
		validGroup.setId(null);
		assertThat(newList, equalTo(oldList));

	}

	@Test
	public void testGroupWithEmptyDataCanBeCreated() throws Exception {
		GroupObject emptyDataGroup = new GroupObject().setFooter("")
				.setHeader("").setName("");
		Set<GroupObject> oldList = app.getGroupHelper().getGroups();
		app.getGroupHelper().createGroup(emptyDataGroup);
		Set<GroupObject> newList = app.getGroupHelper().getGroups();
		verifyGroupAdded(oldList, emptyDataGroup, newList);
	}

}